package ${domain}.module.${moduleName}.svr;

import ${domain}.frame.auth.Token;
import ${domain}.frame.base.ErrorType;
import ${domain}.frame.utils.Message;
import ${domain}.frame.utils.ValidationUtil;
<#if table.getCreate()>
import ${domain}.module.${moduleName}.req.${table.getCName()}CreateRequest;
</#if>
<#if table.getDelete()>
import ${domain}.module.${moduleName}.req.${table.getCName()}DeleteRequest;
</#if>
<#if table.getFind()>
import ${domain}.module.${moduleName}.req.${table.getCName()}FindRequest;
</#if>
<#if table.getGet()>
import ${domain}.module.${moduleName}.req.${table.getCName()}GetRequest;
</#if>
<#if table.getSearch()>
import ${domain}.module.${moduleName}.req.${table.getCName()}SearchRequest;
</#if>
<#if table.getUpdate()>
import ${domain}.module.${moduleName}.req.${table.getCName()}UpdateRequest;
</#if>
<#if table.getCreate()>
import ${domain}.module.${moduleName}.rsp.${table.getCName()}CreateResponse;
</#if>
<#if table.getDelete()>
import ${domain}.module.${moduleName}.rsp.${table.getCName()}DeleteResponse;
</#if>
<#if table.getFind()>
import ${domain}.module.${moduleName}.rsp.${table.getCName()}FindResponse;
</#if>
<#if table.getGet()>
import ${domain}.module.${moduleName}.rsp.${table.getCName()}GetResponse;
</#if>
<#if table.getSearch()>
import ${domain}.module.${moduleName}.rsp.${table.getCName()}SearchResponse;
</#if>
<#if table.getUpdate()>
import ${domain}.module.${moduleName}.rsp.${table.getCName()}UpdateResponse;
</#if>
<#list table.methods as item>
<#if item.selected>
import ${domain}.module.${moduleName}.req.${table.getCName()}${item.getAbbName()?default("")}Request;
import ${domain}.module.${moduleName}.rsp.${table.getCName()}${item.getAbbName()?default("")}Response;
</#if>
</#list>
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

/**
 * ${table.tableComment}
 *
 * @author ${author?default("")}
 * @version 0.0.1
 * @since ${.now?string["yyyy-MM-dd"]}
 */
@Service
public class ${table.getCName()}ServiceImpl implements ${table.getCName()}Service {

    private final static String SERVICE = "EXAMPLE-WEB";

    private final static String SERVICE_URL = String.format("http://%s/msvr/${moduleName}/${table.getCName()}/", SERVICE);

    @Autowired
    private RestTemplate restTemplate;
<#if table.getCreate()>

    /**
     * 插入
     *
     * @param request 请求对象
     * @param token   令牌
     * @return 响应
     */
    public ${table.getCName()}CreateResponse create(${table.getCName()}CreateRequest request, Token token) {
        ${table.getCName()}CreateResponse response = new ${table.getCName()}CreateResponse();

        ValidationUtil.validate(request, response);
        if (response.hasError()) {
            return response;
        }
        try {
            response = restTemplate.postForObject(SERVICE_URL + "create", request, NewTableCreateResponse.class);
        } catch (RestClientException e) {
            response.addError(ErrorType.BUSINESS_ERROR, Message.SERVICE_FAILURE);
        }
        return response;
    }
</#if>
<#if table.getDelete()>

    /**
     * 逻辑删除
     *
     * @param request 请求对象
     * @param token   令牌
     * @return 响应
     */
    public ${table.getCName()}DeleteResponse delete(${table.getCName()}DeleteRequest request, Token token) {
        ${table.getCName()}DeleteResponse response = new ${table.getCName()}DeleteResponse();

        ValidationUtil.validate(request, response);
        if (response.hasError()) {
            return response;
        }
        return response;
    }
</#if>
<#if table.getUpdate()>

    /**
     * 更新
     *
     * @param request 请求对象
     * @param token   令牌
     * @return 响应
     */
    public ${table.getCName()}UpdateResponse update(${table.getCName()}UpdateRequest request, Token token) {
        ${table.getCName()}UpdateResponse response = new ${table.getCName()}UpdateResponse();

        ValidationUtil.validate(request, response);
        if (response.hasError()) {
            return response;
        }
        return response;
    }
</#if>
<#if table.getFind()>

    /**
     * 查询
     *
     * @param request 请求对象
     * @param token   令牌
     * @return 响应
     */
    public ${table.getCName()}FindResponse find(${table.getCName()}FindRequest request, Token token) {
        ${table.getCName()}FindResponse response = new ${table.getCName()}FindResponse();

        ValidationUtil.validate(request, response);
        if (response.hasError()) {
            return response;
        }
        return response;
    }
</#if>
<#if table.getGet()>

    /**
     * 获得对象
     *
     * @param request 请求对象
     * @param token   令牌
     * @return 响应
     */
    public ${table.getCName()}GetResponse get(${table.getCName()}GetRequest request, Token token) {
        ${table.getCName()}GetResponse response = new ${table.getCName()}GetResponse();

        ValidationUtil.validate(request, response);
        if (response.hasError()) {
            return response;
        }
        return response;
    }
</#if>
<#if table.getSearch()>

    /**
     * 模糊查询
     *
     * @param request 请求对象
     * @param token   令牌
     * @return 响应
     */
    public ${table.getCName()}SearchResponse search(${table.getCName()}SearchRequest request, Token token) {
	    ${table.getCName()}SearchResponse response = new ${table.getCName()}SearchResponse();

        ValidationUtil.validate(request, response);
        if (response.hasError()) {
            return response;
        }
        return response;
    }
</#if>
<#list table.methods as item>
<#if item.selected>

    /**
     * ${item.note?default("")}
     *
     * @param request 请求对象
     * @param token   令牌
     * @return
     */
    public ${table.getCName()}${item.getAbbName()?default("")}Response ${item.name}(${table.getCName()}${item.getAbbName()?default("")}Request request, Token token) {
        ${table.getCName()}${item.getAbbName()?default("")}Response response = new ${table.getCName()}${item.getAbbName()?default("")}Response();

        ValidationUtil.validate(request, response);
        if (response.hasError()) {
            return response;
        }

        return response;
    }
</#if>
</#list>
}
